package com.sgb.member.view_model;

import android.text.TextUtils;
import android.view.View;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.BiddingRegisterBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.ui.fragment.BiddingQuotationFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;


public class BidderQuotationModel extends BaseViewModel {

    private final BiddingQuotationFragment fragment;
    private final MemberRepertory mRepertory;
    private final int maxPageSize = 10;
    private int current;

    private CommonPopEntity mAll;


    public BidderQuotationModel(BiddingQuotationFragment fragment, MemberRepertory repertory) {
        this.fragment = fragment;
        this.mRepertory = repertory;

        mAll = new CommonPopEntity();
        mAll.setNo("");
        mAll.setTypeName("全部");
    }

    public void showOrHideDialog(boolean aBoolean) {
        if (aBoolean) {
            if (!DialogHelper.isShowing()) {
                DialogHelper.showProgressDialog(fragment.getActivity(), "数据获取中...");
            }
        } else {
            DialogHelper.dismissProgressDialog();
        }
    }

    public void getBidderQuotation(boolean isLoadMore, String companyName, String mNo1, String mNo2, String mNo7, String mNo8, String mNo9) {
        int currentPage = isLoadMore ? current + 1 : 1;
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("page", currentPage);
        map.put("limit", maxPageSize);
        map.put("keyword", TextUtils.isEmpty(companyName) ? "" : companyName);
        map.put("type", mNo1); //招标类型
        map.put("goodsType", mNo2); //类型，永远取最新的
        map.put("verticalType", ""); //招标方式
        map.put("price", mNo7); //概算金额
        map.put("province", mNo8);
        map.put("city", mNo9);
        mRepertory.getApi().getBidQuoteList(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<BiddingRegisterBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<BiddingRegisterBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                fragment.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<BiddingRegisterBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                if (!isLoadMore) {
                    fragment.showEmpty();
                }
            }

            @Override
            public void onFinally() {
                showOrHideDialog(false);
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(int type) {
        showOrHideDialog(true);
        mRepertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                showOrHideDialog(false);
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setChooseItemData(type, entities);
                    fragment.setAddressData(data.getData());
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no, int type, boolean isShowLeftDialog) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new CommonPopEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, entities);
                    } else {
                        fragment.setChooseItemData(type, entities);
                    }
                }
            }
        }
    }

    //招标状态
    public void getProjectStatus(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("areacode", mRepertory.getAreaCode());
        mRepertory.getApi().getProjectStatus(map).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> list = new ArrayList<>();
                    for (CommonPopEntity datum : data.getData()) {
                        if (!"6".equals(datum.getNo())) {
                            list.add(datum);
                        }
                    }
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, list);
                    } else {
                        fragment.setChooseItemData(type, list);
                    }
                }
            }
        });
    }

    //招标方式 （资格预审、资格后审、报名即报价）
    public void getTenderSortSelect(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getTenderSortSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }


    //报名开始时间
    public void getEnlistStartTimeList(int type, boolean isShowLeftDialog) {
//        String[] stringTime = new String[]{"全部", "今天&1", "3天内&3", "5天内&5", "10天内&10", "30天内&30", "30天以上&31"};
        List<CommonPopEntity> list = new ArrayList<>();
        list.add(mAll);
        list.add(new CommonPopEntity("1", "今天"));
        list.add(new CommonPopEntity("3", "3天内"));
        list.add(new CommonPopEntity("5", "5天内"));
        list.add(new CommonPopEntity("10", "10天内"));
        list.add(new CommonPopEntity("30", "30天内"));
        list.add(new CommonPopEntity("31", "30天以上"));
        if (isShowLeftDialog) {
            fragment.showLeftDialog(type, list);
        } else {
            fragment.setChooseItemData(type, list);
        }
    }

    //招标概算金额
    public void getProjectMoney(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().getProjectMoney().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    // 竞价类型 (商品材料、专业工程、劳务分包...)
    public void getTenderNewTypeList(int type, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        mRepertory.getApi().biddingTypeList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    // 招标类型->获取类别下级所有树形数据
    public void getTenderGoodsCategoryAllList(int type, String category, boolean isShowLeftDialog) {
        showOrHideDialog(true);
        Map<String, Object> map = new HashMap<>();
        map.put("type", category);
        mRepertory.getApi().getTenderGoodsCategoryAllList(map).enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                if (data.getData() != null && data.getData() != null && data.getData().size() > 0) {
                    if (isShowLeftDialog) {
                        fragment.showLeftDialog(type, data.getData());
                    } else {
                        fragment.setChooseItemData(type, data.getData());
                    }
                }
            }
        });
    }

    public void getUrlSetting(String id) {
        showOrHideDialog(true);
        mRepertory.getApi().getUrlSetting("competitivePurchase").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    MemberConfig.getInstance().startWebViewX5(fragment.getActivity(), url);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

}
